/*
 * $RCSfile: DefaultDataExport.java,v $$
 * $Revision: 1.1 $
 * $Date: 2013-3-12 $
 *
 * Copyright (C) 2008 Skin, Inc. All rights reserved.
 *
 * This software is the proprietary information of Skin, Inc.
 * Use is subject to license terms.
 */
package com.skin.webcat.exchange;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

import com.skin.webcat.database.Column;
import com.skin.webcat.database.Table;
import com.skin.webcat.database.handler.TableHandler;
import com.skin.webcat.exchange.transform.DataTransform;
import com.skin.webcat.exchange.transform.DateTransform;
import com.skin.webcat.exchange.transform.NumberTransform;
import com.skin.webcat.exchange.transform.StringTransform;
import com.skin.webcat.util.StringUtil;

/**
 * <p>Title: DefaultDataExport</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * @author xuesong.net
 * @version 1.0
 */
public class DefaultDataExport extends DataExport {
    /**
     *
     */
    public DefaultDataExport() {
        super();
    }

    /**
     * @param connection
     */
    public DefaultDataExport(Connection connection) {
        super(connection);
    }

    /**
     * @param tableName
     * @param out
     * @throws Exception
     */
    @Override
    public void execute(String tableName, PrintWriter out) throws Exception {
        String name = StringUtil.replace(tableName, "'", "");

        int k = name.indexOf(" ");

        if(k > -1) {
            name = name.substring(0, k);
        }

        PreparedStatement statement = null;
        ResultSet resultSet = null;
        
        if(this.getHeader()) {
            out.println("## generated by dataexchange v1.0");
            out.println(name);
        }

        StringBuilder sql = new StringBuilder("select * from ");
        sql.append(name);

        statement = this.connection.prepareStatement(sql.toString());
        resultSet = statement.executeQuery();
        ResultSetMetaData metaData = resultSet.getMetaData();

        TableHandler tableHandler = new TableHandler(this.connection);
        Table table = tableHandler.getTable(metaData);
        List<Column> columns = table.getColumns();
        List<DataTransform> transforms = new ArrayList<DataTransform>(columns.size());

        for(Column column : columns) {
            String javaTypeName = column.getJavaTypeName();

            if(javaTypeName.equalsIgnoreCase("boolean") || javaTypeName.equalsIgnoreCase("Boolean")
                    || javaTypeName.equalsIgnoreCase("byte") || javaTypeName.equalsIgnoreCase("Byte")
                    || javaTypeName.equalsIgnoreCase("short") || javaTypeName.equalsIgnoreCase("Short")
                    || javaTypeName.equalsIgnoreCase("int") || javaTypeName.equalsIgnoreCase("Integer")
                    || javaTypeName.equalsIgnoreCase("float") || javaTypeName.equalsIgnoreCase("Float")
                    || javaTypeName.equalsIgnoreCase("double") || javaTypeName.equalsIgnoreCase("Double")
                    || javaTypeName.equalsIgnoreCase("long") || javaTypeName.equalsIgnoreCase("Long")
            ) {
                transforms.add(new NumberTransform());
            }
            else if(javaTypeName.equalsIgnoreCase("String")) {
                transforms.add(new StringTransform());
            }
            else if(javaTypeName.equalsIgnoreCase("java.util.Date")) {
                transforms.add(new DateTransform());
            }
            else {
                transforms.add(new StringTransform());
            }
        }

        int i = 0;
        int size = columns.size();

        if(this.getHeader()) {
            for(i = 0; i < size; i++) {
                Column column = columns.get(i);
                out.print(column.getColumnName());
                out.print(":");
                out.print(column.getJavaTypeName());
    
                if(i < (size - 1)) {
                    out.print(",");
                }
            }
    
            out.println();
        }

        size = size - 1;

        while(resultSet.next()) {
            for(i = 0; i < size; i++) {
                Column column = columns.get(i);
                DataTransform transform = transforms.get(i);
                out.print(transform.toString(resultSet.getObject(column.getColumnName())));
                out.print(",");
            }

            if(size > -1) {
                Column column = columns.get(i);
                DataTransform transform = transforms.get(i);
                out.print(transform.toString(resultSet.getObject(column.getColumnName())));
                out.println();
            }
        }
        out.flush();
    }
}
